package demo.java.parallelprogram.queue;

public class SyncCounter implements  Runnable {

	private static int count;
	
	public SyncCounter(){
		count=0;
	}
	
	public void countAdd(){
		synchronized (this) {
			for(int i=0;i<5;i++){
				try {
					System.out.println(Thread.currentThread().getName()+";=="+count++);
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void printCount(){
		for(int i=0;i<5;i++){
			try {
				System.out.println(Thread.currentThread().getName()+";count:"+count);
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void run() {
		synchronized (this) {
			for(int i=0;i<5;i++){
				try {
					System.out.println(Thread.currentThread().getName()+";=="+count++);
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static int getCount() {
		return count;
	}
	
	
	public static void main(String[] args) {
		SyncCounter syncThread = new SyncCounter();
		Thread thread1 = new Thread(syncThread, "A");
		Thread thread2 = new Thread(syncThread, "B");
		thread1.start();
		thread2.start();
	}
}
